package com.example.persistence;

import java.util.HashSet;
import java.util.Set;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.R;
import com.example.entity.SelectMode;
import com.example.entity.ViewHolder;
import com.example.util.ViewUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
 */
public abstract class ImageActivity extends BaseActivity {
	private static final long SLIDE_DURATION = 300;

	protected abstract void shareTwitter(Set<String> selectedItem);
	protected abstract void shareKakaoTalk(Set<String> selectedItem);
	protected abstract void shareFacebook(Set<String> selectedItem);
	protected abstract void doDelete(Set<String> selectedItem);

	protected ImageLoader imageLoader = ImageLoader.getInstance();
	private boolean instanceStateSaved;
	protected SelectMode selectMode;
	private Set<String> selectedItem;
	
	private LinearLayout topBar, shareBar, actionBar;
	private ImageView shareButton, deleteButton;
	private ImageView kakaoButton,  twitterButton, facebookButton;
	private ImageView permitButton, cancelButton;
	private FrameLayout itemWrapper;
	private LinearLayout contentWapper;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		selectMode = SelectMode.NONE;
		selectedItem = new HashSet<String>();
		
		super.onCreate(savedInstanceState);
	}

	public void wrapContentView(ViewGroup root, View view) {
		root.removeView(view);
		contentWapper = (LinearLayout) getLayoutInflater().inflate(R.layout.content_wrapper, null);
		itemWrapper = (FrameLayout) contentWapper.findViewById(R.id.itemWrapper);
		itemWrapper.addView(view);
		root.addView(contentWapper);
		
		topBar = (LinearLayout) contentWapper.findViewById(R.id.topBar);
        shareBar = (LinearLayout) contentWapper.findViewById(R.id.shareBar);
        actionBar = (LinearLayout) contentWapper.findViewById(R.id.actionBar);
        
        shareButton = (ImageView) contentWapper.findViewById(R.id.shareButton);
        shareButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				displayShareBar();
			}
		});
        deleteButton = (ImageView) contentWapper.findViewById(R.id.deleteButton);
        deleteButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				displayActionBar();
			}
		});
        
        permitButton = (ImageView) contentWapper.findViewById(R.id.permitButton);
        permitButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				doDelete(selectedItem);
			}
		});
        
        cancelButton = (ImageView) contentWapper.findViewById(R.id.cancelButton);
        cancelButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				displayTopBar();
			}
		});
        
        kakaoButton = (ImageView) contentWapper.findViewById(R.id.kakaoButton);
        kakaoButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if (selectedItem == null || selectedItem.size() < 1)
					return;
				
				shareKakaoTalk(selectedItem);
			}
		});
        
        facebookButton = (ImageView) contentWapper.findViewById(R.id.facebookButton);
        facebookButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				shareFacebook(selectedItem);
			}
		});
        
		twitterButton = (ImageView) contentWapper.findViewById(R.id.twitterButton);
        twitterButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				shareTwitter(selectedItem);	
			}
		});
        
        contentWapper.post(new Runnable() {
			@Override
			public void run() {
				FrameLayout.LayoutParams wrapperParams = (FrameLayout.LayoutParams) contentWapper.getLayoutParams();
				wrapperParams.height = contentWapper.getHeight() + topBar.getHeight();
				contentWapper.setLayoutParams(wrapperParams);
		        
		        ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) itemWrapper.getLayoutParams();
				params.height = itemWrapper.getHeight();
				itemWrapper.setLayoutParams(params);
			}
		});
	}

	public void displayTopBar() {
		selectMode = SelectMode.NONE;
		
		TranslateAnimation translate = new TranslateAnimation(0, 0, -topBar.getHeight(), 0);
        translate.setDuration(SLIDE_DURATION);
        
        if (application.getSdkVersion() > 15)
        	translate.setFillBefore(false);
        else
        	translate.setFillBefore(true);
        	
        translate.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				int height = -topBar.getHeight();
				
				if (application.getSdkVersion() > 15)
					contentWapper.scrollBy(0, height);
				else {
					topBar.setVisibility(View.VISIBLE);
					contentWapper.layout(contentWapper.getLeft(), contentWapper.getTop() + height, contentWapper.getRight(), contentWapper.getBottom() + height);	
				}
			}
			@Override
			public void onAnimationRepeat(Animation animation) {}
			@Override
			public void onAnimationEnd(Animation animation) {
				actionBar.setVisibility(View.GONE);
		        shareBar.setVisibility(View.GONE);
		        contentWapper.clearAnimation();
			}
		});
        
        contentWapper.startAnimation(translate);
	}
	
	public void displayShareBar() {
		selectMode = SelectMode.SINGLE;
		
		TranslateAnimation translate = new TranslateAnimation(0, 0, 0, -topBar.getHeight());
        translate.setDuration(SLIDE_DURATION);
        
        if (application.getSdkVersion() > 15)
        	translate.setFillBefore(false);
        else
        	translate.setFillBefore(true);
        
        translate.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				shareBar.setVisibility(View.VISIBLE);
				actionBar.setVisibility(View.GONE);
			}
			@Override
			public void onAnimationRepeat(Animation animation) {}
			@Override
			public void onAnimationEnd(Animation animation) {
				if (application.getSdkVersion() > 15)
					contentWapper.scrollBy(0, topBar.getHeight());
				else
					topBar.setVisibility(View.GONE);
				
				contentWapper.clearAnimation();
			}
		});
        
        contentWapper.startAnimation(translate);
	}

	public void displayActionBar() {
		selectMode = SelectMode.MULTIPLE;
		
		TranslateAnimation translate = new TranslateAnimation(0, 0, 0, -topBar.getHeight());
        translate.setDuration(SLIDE_DURATION);
        
        if (application.getSdkVersion() > 15)
        	translate.setFillBefore(false);
        else
        	translate.setFillBefore(true);
        
        translate.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				actionBar.setVisibility(View.VISIBLE);
				shareBar.setVisibility(View.GONE);
			}
			@Override
			public void onAnimationRepeat(Animation animation) {}
			@Override
			public void onAnimationEnd(Animation animation) {
				if (application.getSdkVersion() > 15)
					contentWapper.scrollBy(0, topBar.getHeight());
				else
					topBar.setVisibility(View.GONE);
				
				contentWapper.clearAnimation();
			}
		});
        
        contentWapper.startAnimation(translate);
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		instanceStateSaved = true;
	}

	@Override
	protected void onDestroy() {
		if (!instanceStateSaved) {
			imageLoader.stop();
		}
		super.onDestroy();
	}
	
	@Override
	public void onBackPressed() {
		Log.d("ImageActivity onBackPressed", "selectMode=" + selectMode);
		if (selectMode != SelectMode.NONE) {
			displayTopBar();
			deselectAllItems();
		}
		else
			super.onBackPressed();
	}
	
	public void selectSingleItem(String key, View view) {
		deselectAllItems();
		selectItem(key, view);
	}
	
	public void selectMultipleItem(String key, View view) {
		if (isSelectedItem(key))
			deselectItem(key, view);
		else
			selectItem(key, view);
	}
	
	public void deselectAllItems() {
		selectedItem.clear();
		deselectAllViews();
	}
	
	public void selectItem(String key, View view) {
		selectedItem.add(key);
		ViewHolder viewHolder = (view.getTag() == null) ? new ViewHolder() : (ViewHolder) view.getTag();
		viewHolder.selected = true;
		viewHolder.checkOverlay.setVisibility(View.VISIBLE);
	}
	
	public void deselectItem(String key, View view) {
		selectedItem.remove(key);
		ViewHolder viewHolder = (view.getTag() == null) ? new ViewHolder() : (ViewHolder) view.getTag();
		viewHolder.selected = false;
		viewHolder.checkOverlay.setVisibility(View.GONE);
	}
	
	public boolean isSelectedItem(String key) {
		return selectedItem.contains(key);
	}
	
	public void deselectAllViews() {
		for (View view : ViewUtil.findViewsByStatus(itemWrapper, true)) {
			ViewHolder viewHolder = (com.example.entity.ViewHolder) view.getTag();
			if (viewHolder != null) {
				viewHolder.selected = false;
				viewHolder.checkOverlay.setVisibility(View.GONE);
			}
		}
	}
}
